<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Intaichinh_model extends CI_Model
{
	function __construct(){
        parent::__construct();
	}
	
	public function initialise($lang)
    {
		$this->lang = $lang;
    }
	
	function get_all($tbl, $num=20, $start=0)
	{
		$this->db->where('bl_active', 1) ;
		$query = $this->db->get($tbl, $num, $start);
		return $query->result() ;
	}
	
	function getProFromCat($table,$cat_id,$num,$offset)
	{
		$this->db->select('pro_id, name_'.$this->lang.' as lb_name, lb_image');
		$this->db->where('bl_active', 1) ;
		$this->db->where('catid',$cat_id);
		$this->db->order_by('nb_order','ASC');
		$result=$this->db->get($table,$num,$offset)->result();
		return $result;
	}
	
	function count_all($tbl)
	{
		$this->db->where('bl_active', 1) ;
		$query = $this->db->get($tbl);
		return $query->num_rows() ;
	}

	function get_id($tbl, $name, $id)
	{
		if($id) $this->db->where($name, $id) ;
		$this->db->where('bl_active', 1) ;
		$query = $this->db->get($tbl);
		return $query->row() ;
	}
	
	function save($tbl, $data, $name='', $id=0)
	{
		if($name)
		{
			$this->db->where($name, $id) ;
			if($this->db->update($tbl, $data))
				return $id ;
			else return false;
		}
		else
		{
			if($this->db->insert($tbl,$data))
				return $this->db->insert_id();
			else return false;
		}
	}
	
	function del($tbl, $id, $name)
	{
		if($id){
			$this->db->where($name, $id) ;
			$this->db->delete($tbl) ;
		}
	}
	
	function trash($tbl, $id, $name)
	{
		if($id){
			$this->db->where($name, $id);
			return $this->db->update($tbl, array('bl_active'=>-1));
		}
	}
	
	function restore($tbl, $id, $name)
	{
		if($id){
			$this->db->where($name, $id);
			return $this->db->update($tbl, array('bl_active'=>1));
		}
	}
	
	function remove($tbl)
	{
		$date = new DateTime();
		$date->modify('-3 month');
		$date_del = $date->format('Y-m-d H:i:s');
		
		$this->db->where('bl_active', -1);
		$this->db->where('dt_create <', $date_del);
		if($this->db->delete($tbl)){
		  	return true;
		}else{
		  	return false;
		}
	}
	
	function get_result($tbl, $num=10, $start=0, $wh='', $value=0)
	{
		if($wh) $this->db->where($wh, $value) ;
		$this->db->where('bl_active', 1) ;
		$query = $this->db->get($tbl, $num, $start);
		return $query->result() ;
	}
	
	function get_where($tbl, $wh='', $value=0, $num=100, $start=0, $order='')
	{
		if($wh) $this->db->where($wh, $value) ;
		if($order) $this->db->order_by($order, 'DESC') ;
		$this->db->where('bl_active', 1) ;
		$query = $this->db->get($tbl, $num, $start);
		return $query->result() ;
	}

	function count_where($tbl, $where='', $id=0)
	{
		if($where) $this->db->where($where, $id) ;
		$this->db->where('bl_active', 1) ;
		$query = $this->db->get($tbl);
		return $query->num_rows() ;
	}
	
	function get_name($tbl, $field, $wh='', $value=0)
	{
		$this->db->select($field) ;
		if($wh) $this->db->where($wh, $value) ;
		$this->db->where('bl_active', 1) ;
		$query = $this->db->get($tbl);
		return $query->row() ;
	}
	
	function get_field($tbl, $field, $wh='', $value=0)
	{
		$this->db->select($field) ;
		if($wh) $this->db->where($wh, $value) ;
		$this->db->where('bl_active', 1) ;
		$rs = $this->db->get($tbl)->row();
		if($rs)
			return $rs->$field ;
		else
			return ;
	}
}
?>